package cn.com.pagoda.ideplugin.ui;

import com.intellij.openapi.actionSystem.AnActionEvent;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 马超 on 2023-05-06
 */
public class PwdGenPanel {

    private final AnActionEvent anActionEvent;

    public PwdGenPanel(AnActionEvent anActionEvent, JPanel mainPanel) {
        this.anActionEvent = anActionEvent;

        JPanel pwdGenPanel = new JPanel();
        placeComponents(pwdGenPanel);
        mainPanel.add(pwdGenPanel, PwdGenPanel.class.getSimpleName());
    }

    private static void placeComponents(JPanel panel) {
        panel.setLayout(null);

        JLabel passwordLengthLabel = new JLabel("password length:");
        passwordLengthLabel.setBounds(210, 20, 100, 25);
        panel.add(passwordLengthLabel);

        JSlider passwordLengthSlider = new JSlider(JSlider.HORIZONTAL, 4, 64, 8);
        passwordLengthSlider.setBounds(310, 20, 190, 25);
        panel.add(passwordLengthSlider);

        JLabel sliderValueLabel = new JLabel(String.valueOf(passwordLengthSlider.getValue()));
        sliderValueLabel.setBounds(520, 20, 50, 25);
        panel.add(sliderValueLabel);

        passwordLengthSlider.addChangeListener(e -> {
            JSlider source = (JSlider) e.getSource();
            sliderValueLabel.setText(String.valueOf(source.getValue()));
        });

        // 提示栏
        JLabel tipsLabel = new JLabel("length equals 0,then random length");
        tipsLabel.setForeground(Color.blue);
        tipsLabel.setBounds(210, 50, 250, 25);
        panel.add(tipsLabel);

        JCheckBox digitsCheckBox = new JCheckBox("digit");
        digitsCheckBox.setSelected(true);// 默认选中
        digitsCheckBox.setBounds(210, 80, 90, 25);
        panel.add(digitsCheckBox);
        JLabel digitsLengthLabel = new JLabel("length:");
        digitsLengthLabel.setBounds(310, 80, 40, 25);
        panel.add(digitsLengthLabel);

        JSlider digitsSlider = new JSlider(JSlider.HORIZONTAL, 0, 20, 8);
        digitsSlider.setBounds(350, 80, 150, 25);
        panel.add(digitsSlider);
        JLabel digitsSliderValueLabel = new JLabel(String.valueOf(digitsSlider.getValue()));
        digitsSliderValueLabel.setBounds(520, 80, 50, 25);
        panel.add(digitsSliderValueLabel);
        digitsSlider.addChangeListener(e -> {
            JSlider source = (JSlider) e.getSource();
            digitsSliderValueLabel.setText(String.valueOf(source.getValue()));
        });

        JCheckBox lowercaseCheckBox = new JCheckBox("lowercase");
        lowercaseCheckBox.setBounds(210, 110, 90, 25);
        panel.add(lowercaseCheckBox);
        JLabel lowercaseLengthLabel = new JLabel("length:");
        lowercaseLengthLabel.setBounds(310, 110, 40, 25);
        panel.add(lowercaseLengthLabel);


        JSlider lowercaseSlider = new JSlider(JSlider.HORIZONTAL, 0, 20, 0);
        lowercaseSlider.setBounds(350, 110, 150, 25);
        panel.add(lowercaseSlider);
        JLabel lowercaseSliderValueLabel = new JLabel(String.valueOf(lowercaseSlider.getValue()));
        lowercaseSliderValueLabel.setBounds(520, 110, 50, 25);
        panel.add(lowercaseSliderValueLabel);

        lowercaseSlider.addChangeListener(e -> {
            JSlider source = (JSlider) e.getSource();
            lowercaseSliderValueLabel.setText(String.valueOf(source.getValue()));
        });

        JCheckBox uppercaseCheckBox = new JCheckBox("uppercase");
        uppercaseCheckBox.setBounds(210, 140, 90, 25);
        panel.add(uppercaseCheckBox);
        JLabel uppercaseLengthLabel = new JLabel("length:");
        uppercaseLengthLabel.setBounds(310, 140, 40, 25);
        panel.add(uppercaseLengthLabel);

        JSlider uppercaseSlider = new JSlider(JSlider.HORIZONTAL, 0, 20, 0);
        uppercaseSlider.setBounds(350, 140, 150, 25);
        panel.add(uppercaseSlider);
        JLabel uppercaseSliderValueLabel = new JLabel(String.valueOf(uppercaseSlider.getValue()));
        uppercaseSliderValueLabel.setBounds(520, 140, 50, 25);
        panel.add(uppercaseSliderValueLabel);

        uppercaseSlider.addChangeListener(e -> {
            JSlider source = (JSlider) e.getSource();
            uppercaseSliderValueLabel.setText(String.valueOf(source.getValue()));
        });

        JCheckBox specialCheckBox = new JCheckBox("special");
        specialCheckBox.setBounds(210, 170, 90, 25);
        panel.add(specialCheckBox);
        JLabel specialLengthLabel = new JLabel("length:");
        specialLengthLabel.setBounds(310, 170, 40, 25);
        panel.add(specialLengthLabel);

        JSlider specialSlider = new JSlider(JSlider.HORIZONTAL, 0, 20, 0);
        specialSlider.setBounds(350, 170, 150, 25);
        panel.add(specialSlider);
        JLabel specialSliderValueLabel = new JLabel(String.valueOf(specialSlider.getValue()));
        specialSliderValueLabel.setBounds(520, 170, 50, 25);
        panel.add(specialSliderValueLabel);

        specialSlider.addChangeListener(e -> {
            JSlider source = (JSlider) e.getSource();
            specialSliderValueLabel.setText(String.valueOf(source.getValue()));
        });


        JButton generateButton = new JButton("generate");
        generateButton.setBounds(210, 230, 120, 25);
        panel.add(generateButton);

        JLabel resultLabel = new JLabel("result:");
        resultLabel.setBounds(350, 230, 50, 25);
        panel.add(resultLabel);

        JTextField resultTextField = new JTextField(20);
        resultTextField.setBounds(390, 230, 160, 25);
        panel.add(resultTextField);

        generateButton.addActionListener(e -> {
            int passwordLength = passwordLengthSlider.getValue();

            int digitsLength = digitsCheckBox.isSelected() ? digitsSlider.getValue() : 0;
            int lowercaseLength = lowercaseCheckBox.isSelected() ? lowercaseSlider.getValue() : 0;
            int uppercaseLength = uppercaseCheckBox.isSelected() ? uppercaseSlider.getValue() : 0;
            int specialLength = specialCheckBox.isSelected() ? specialSlider.getValue() : 0;

            cn.com.pagoda.ideplugin.service.pwdGen.service.PwdReq pwdReq = new cn.com.pagoda.ideplugin.service.pwdGen.service.PwdReq();
            pwdReq.setTotal(passwordLength);
            Map<String, Integer> generators = new HashMap<>();
            pwdReq.setGenerators(generators);

            // 如果复选框被选中，且长度>0, 则认为用户设置了长度； 被选中且长度=0，则认为客户想要随机长度
            Integer verifyLength = 0;
            Integer randomtype = 0;
            if (digitsCheckBox.isSelected()) {
                generators.put("NUMBER", digitsLength);
                if (digitsLength > 0) {
                    verifyLength += digitsLength;
                }
                if (digitsLength == 0) {
                    randomtype++;
                }
            }
            if (lowercaseCheckBox.isSelected()) {
                generators.put("LOWER", lowercaseLength);
                if (lowercaseLength > 0) {
                    verifyLength += lowercaseLength;
                }
                if (lowercaseLength == 0) {
                    randomtype++;
                }
            }
            if (uppercaseCheckBox.isSelected()) {
                generators.put("UPPER", uppercaseLength);
                if (uppercaseLength > 0) {
                    verifyLength += uppercaseLength;
                }
                if (uppercaseLength == 0) {
                    randomtype++;
                }
            }
            if (specialCheckBox.isSelected()) {
                generators.put("SPECIAL", specialLength);
                if (specialLength > 0) {
                    verifyLength += specialLength;
                }
                if (specialLength == 0) {
                    randomtype++;
                }
            }

            if (randomtype == 0 && verifyLength == 0) {
                JOptionPane.showMessageDialog(panel, "Please select at least one type", "ERROR", JOptionPane.ERROR_MESSAGE);
                return;
            }
            int remin = passwordLength - verifyLength;
            if (randomtype == 0 && remin > 0) {
                JOptionPane.showMessageDialog(panel, "Total password length is not equal to the length of the selected types", "ERROR", JOptionPane.ERROR_MESSAGE);
                return;
            }
            if (remin < randomtype) {
                JOptionPane.showMessageDialog(panel, "Total password length is less than the length of the selected types", "ERROR", JOptionPane.ERROR_MESSAGE);
                return;
            }

            // 生成密码
            String generator = new cn.com.pagoda.ideplugin.service.pwdGen.PasswordGeneratorService().createGenerator(pwdReq);
            resultTextField.setText(generator);
        });

        JButton copyButton = new JButton("copy");
        copyButton.setBounds(465, 280, 80, 25);
        panel.add(copyButton);

        copyButton.addActionListener(e -> {
            StringSelection stringSelection = new StringSelection(resultTextField.getText());
            Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
            clipboard.setContents(stringSelection, null);
        });
    }
}
